import { Injectable } from '@nestjs/common';
import { ReturnModelType } from '@typegoose/typegoose';
import { InjectModel } from 'nestjs-typegoose/dist/typegoose.decorators';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { Post as PostSchema } from './entities/post.entity';

@Injectable()
export class PostsService {
  constructor(
    @InjectModel(PostSchema)
    private readonly postModel: ReturnModelType<typeof PostSchema>,
  ) {}
  async create(createPostDto: CreatePostDto) {
    const res = await this.postModel.create(createPostDto);
    return res;
  }

  async findAll() {
    return await this.postModel.find();
  }

  async findOne(id: string) {
    return await this.postModel.findById(id);
  }

  async update(id: string, updatePostDto: UpdatePostDto) {
    return await this.postModel.findByIdAndUpdate(id, updatePostDto);
  }

  async remove(id: string) {
    return await this.postModel.findByIdAndRemove(id);
  }
}
